import CimEntity from "./CimEntity";

export function initializeScene(cameraPosition, cimScene) {

    return bfsInCim(cimScene, cameraPosition)
}

export function updateScene(cameraPosition, cimScene) {
    return bfsInCim(cimScene, cameraPosition)
}

function calDistance(pos1, pos2) {

    return Math.sqrt(Math.pow(pos1.x - pos2.x, 2) + Math.pow(pos1.y - pos2.y, 2) + Math.pow(pos1.z - pos2.z, 2))
}

function bfsInCim(cimScene, cameraPos) {

    let lodList = cimScene.lod
    //操作用的队列
    let queue = []
    //实际加载模型用的队列
    let showQueue = []
    queue.push(cimScene)
    while (queue.length) {
        let cimEntity = queue.shift()

        if(cimEntity.type == "other"  || cimEntity.scene_id ){
             // 为other节点或者场景节点
             // scene必有下一级，other没有下一级的时候不做操作
            if (cimEntity.children.length) {
                cimEntity.children.forEach((child) => {
                    queue.push(child)
                })
            }
        }
        else if (calDistance(cameraPos, cimEntity.position) < lodList[cimEntity.lod].distance) {

            //有下一级
            if (cimEntity.children.length) {
                cimEntity.children.forEach((child) => {
                    queue.push(child)
                })
            } else {
                //没有下一级
                //必然是cimBBuilding
                showQueue.unshift(cimEntity)
            }

        } else {
            //距离过远
            showQueue.unshift(cimEntity)
        }
    }
    //console.log('需要加载的内容为', showQueue);
    return showQueue

}